//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef __LINE_GRAPH_YG_H__
#define __LINE_GRAPH_YG_H__

#include "def.h"
#include <wx/timer.h>


class CLineGraph: public wxPanel
{
public:
								CLineGraph(wxWindow* parent, size_t stMaxX=512, wxWindowID id=wxID_ANY);
	virtual 					~CLineGraph();
	
	void						SetUpdateInterVal(unsigned int nUpdateTime);

	void						Reset();

	enum cGraphType				{ cGraphTypeBar=0, cGraphTypeLine };
	enum cGraphLabelPosition	{ cGraphLabelPositionNone=0, cGraphLabelPositionLeft, cGraphLabelPositionRight };

	typedef struct tagGRAPHVISUAL
	{
		cGraphType				cType;
		cGraphLabelPosition		cPos;
		ZONELIST				lstColors;
		wxString				strMesLabel;
		int						nPenWidth;
	}GRAPHVISUAL;

	typedef struct tagDATAENTRY
	{
		tagDATAENTRY():bCompressed(false), t(0)
		{
		}

		time_t		  		t;
		bool				bCompressed;

		LINEVALS			vals;
	}DATAENTRY;

	typedef std::list<DATAENTRY>	GRAPHDATA;

	

	typedef struct tagGRAPH
	{
		GRAPHVISUAL				visual;
		GRAPHDATA				data;
		size_t					stMaxY;
	}GRAPH;

	typedef std::map<wxString, GRAPH>	GRAPHDATASTORAGE;




	void						SetUpGraph(const wxString& strName, size_t stYMax, const GRAPHVISUAL& visual  );

	void						SetMaxX(size_t stX=512)		{ m_stMaxX = stX; }
	void						SetMaxY(size_t stY=16)		{ m_stMaxY = stY; }
	
	void						SetData(const wxString& strName, const LINEVALS& vals);

	void						SetTimePeriod(int nPer);
	int							GetTimePeriod()			{ return m_nTimePeriodSec; }

	void						SetZonesHorizontal(const ZONELIST& m_lstZoneHorizontal);

	void						SetMesureNameY(const wxString& str)			{ m_strMesureItemName=str;			}
	void						SetMesureNameXTOP(const wxString& str)		{ m_strMesureItemNameXTop=str;		}
	void						SetMesureNameXBottom(const wxString& str)	{ m_strMesureItemNameXBottom=str;	}
	void						SetMesureXTopScale(double scale=4.3125 )	{ m_dXBottomToTop = scale;			}

protected:
	void						OnSize(wxSizeEvent& event);
	void						OnPaint(wxPaintEvent& event);
	void 						OnTimer(wxTimerEvent& event);
	

	
private:
	void						CompressData(GRAPHDATA&	data);

	void						DrawGrid( class wxBufferedPaintDC& dc, wxRect&  rectGraphArea);
	void						DrawGridLines(class wxBufferedPaintDC& dc, int x, int y, int w, int h);

	void						DrawXLabels(class wxBufferedPaintDC& dc, int x, int y, int w, int h, bool bSimple);
	void						DrawYLabels(class wxBufferedPaintDC& dc, int x, int y, int w, int h, int nMaxY);
	void						DrawYLabelsRight(class wxBufferedPaintDC& dc, int x, int y, int w, int h, int nMaxY);

	void						DrawLegend( class wxBufferedPaintDC& dc, int&  w, int&  h);
	void						DrawLegend(  wxBufferedPaintDC& dc, int x, int& w, int& h, bool bDraw);
	void						DrawLegendBottom(  wxBufferedPaintDC& dc, int x, int& w, int& h, bool bDraw);


	void						DrawZones(class wxBufferedPaintDC& dc, int x, int y, int w, int h, const wxRect&  rectGraphArea);
	void						DrawGraph(class wxBufferedPaintDC& dc, const GRAPH& graph, const wxRect& rect);
	void						DrawLineGraph(class wxBufferedPaintDC& dc, const GRAPH& graph, const wxRect& rect);

	unsigned int				CalculateUpLevel();
	void						CalculateBlockParams(int nLineLength, int* nBlocksMap,int nMapSize, int nBlockMinSize, int &nBlockSize, int& nBlockCount, size_t stMax);

	wxString					FormatLabel( unsigned long lVal);
	wxString					FormatXLabel(int nTimeBlockIndex, bool bBottom);

	const LINEVALS&				GetData( const GRAPH& graph );
	GRAPH*						GetGraph(const wxString& strName);

private:
	wxTimer						m_timer;
	unsigned int 				m_nUpdateTime;

	size_t						m_stMaxX;
	size_t						m_stMaxY;

	int							m_nTimePeriodSec;

	wxString					m_strMesureItemName;
	wxString					m_strMesureItemNameXTop;
	wxString					m_strMesureItemNameXBottom;
	double						m_dXBottomToTop;

	
	ZONELIST					m_lstZoneHorizontal;

	GRAPHDATASTORAGE			m_graphstorage;

	///////////////////////////////////////////////
	int							m_nTrafBlockSize;
	int							m_nTrafBlockCount;

	int							m_nXBlockSize;
	int							m_nXBlockCount;
};

#endif /*__LINE_GRAPH_YG_H__*/
